home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / dev / gcc / ixemul_src.lha / ixemul-41.0 / libsrc / crt0.c < prev    next >
C/C++ Source or Header  |  1995-06-06  |  10KB  |  365 lines

  1. /* revamped version by Loren J. Rittle. Thanks Loren !! */
  2.  
  3. /* I guess I should start to put my sources into RCS........ */
  4.  
  5. #include <exec/types.h>
  6. #include <exec/libraries.h>
  7. #include <exec/execbase.h>
  8.  
  9. /* have to use explicit 4 dereferencing here, since we can't always access
  10.    globals so early (a4 etc) */
  11. #define BASE_EXT_DECL
  12. #define BASE_EXT_DECL0
  13. #define BASE_NAME *(void **)4
  14. #include <inline/exec.h>
  15. #include <libraries/dosextens.h>
  16. #include <limits.h>
  17. #include "kprintf.h"
  18.  
  19. #include <sys/syscall.h>
  20. #ifdef BASECRT0
  21. #include <sys/exec.h>
  22. #endif /* BASECRT0 */
  23.  
  24. /* get the current revision number. Version control is automatically done by
  25.  * OpenLibrary(), I just have to check the revision number 
  26.  */
  27. #undef IX_VERSION
  28. #include "version.h"
  29.  
  30. #ifdef DEBUG_VERSION
  31. /* #undef IX_NAME */
  32. /* #define IX_NAME "ixkprinf.library" */
  33. #endif
  34.  
  35. #if defined(DEBUG_VERSION) && 0      /* Note:  This doesn't seem to work...  -fnf */
  36. static int inline __geta4() {int res;asm ("movel a4,%0" : "=g" (res));return res;}
  37. #endif
  38.  
  39. #define MSTRING(x) STRING(x)
  40. #define STRING(x) #x
  41.  
  42. struct Library *ixemulbase;
  43.  
  44. static int start_stdio(int, char **, char **);
  45.  
  46. /*
  47.  * Have to take care.. I may not use any library functions in this file,
  48.  * since they are exactly then used, when the library itself couldn't be
  49.  * opened...
  50.  */
  51.  
  52. extern int main();
  53. extern int expand_cmd_line;
  54. extern char *default_wb_window;
  55. extern int errno;
  56. extern char **environ;
  57. extern char *_ctype_;
  58. extern int sys_nerr;
  59. extern unsigned long __SaveSP;
  60. extern struct ExecBase *SysBase;
  61. extern struct Library *DOSBase;
  62. extern struct __sFILE **__sF;
  63. static int ENTRY();
  64. static int exec_entry();
  65.  
  66. /* FIXME:  The "jmp pc@(_ENTRY)" instruction below, when assembled with binutils gas,
  67.    produces a 12 byte instruction that causes the magic number to not be in the
  68.    place expected by execve().  Changing it to "jmp pc@(_ENTRY:W)" generates the
  69.    right 8 byte instruction, but seems to screw something else up so that
  70.    the programs crash immediately or hang.  Need to investigate why.  When fixed,
  71.    need to remove the hack in execve() that allows for either assembled form. */
  72.  
  73. asm("
  74.     .text
  75.  
  76.     jmp    pc@(_ENTRY)    | by default jump to normal AmigaDOS startup
  77.  
  78.     | this is a struct exec, for now only OMAGIC is supported
  79.     .globl    exec
  80. exec:
  81.     .word    ___machtype    | a_mid
  82.     .word    0407        | a_magic = OMAGIC
  83.     .long    ___text_size    | a_text
  84.     .long    ___data_size    | a_data
  85.     .long    ___bss_size    | a_bss
  86.     .long    0        | a_syms
  87.     .long    _exec_entry    | a_entry
  88.     .long    0        | a_trsize
  89.     .long    0        | a_drsize
  90.  
  91.     | word alignment is guaranteed
  92. ");
  93.  
  94. #ifdef BASECRT0
  95. extern int __datadata_relocs();
  96. extern int __data_size, __bss_size;
  97.  
  98. #ifdef RCRT0
  99. /* have to do this this way, or it is done base-relative.. */
  100. static inline int dbsize() 
  101. {
  102.   int res;
  103.   KPRINTF (("enter dbsize()\n"));
  104.   asm ("movel #___data_size,%0; addl #___bss_size,%0" : "=r" (res));
  105.   return res;
  106. }
  107.  
  108. static void inline
  109. ix_resident (void *base, int num, int a4, int size, void *relocs)
  110. {
  111.   typedef void (*func)(int, int, int, void *);
  112.  
  113.   KPRINTF (("enter ix_resident()\n"));
  114.   ((func)((int)base - 6*(SYS_ix_resident + 4))) (num, a4, size, relocs);
  115. }
  116.  
  117. #else
  118. static void inline
  119. ix_resident (void *base, int num, int a4)
  120. {
  121.   typedef void (*func)(int, int);
  122.  
  123.   KPRINTF (("enter ix_resident()\n"));
  124.   ((func)((int)base - 6*(SYS_ix_resident + 4))) (num, a4);
  125. }
  126. #endif
  127. #endif /* BASECRT0 */
  128.  
  129. static int
  130. exec_entry (struct Library *ixembase, int argc, char *argv[], char *env[])
  131. {
  132. #ifdef BASECRT0
  133.   register int a4;
  134.   /* needed, so that data can be accessed. ix_resident might change this
  135.      again afterwards */
  136.   asm volatile ("lea    ___a4_init,a4" : "=r" (a4) : "0" (a4));
  137.   asm volatile ("movel    a4,%0" : "=r" (a4) : "0" (a4));
  138.  
  139.   KPRINTF (("enter exec_entry()\n"));
  140. #ifdef RCRT0
  141.   ix_resident (ixembase, 4, a4, dbsize(), __datadata_relocs);
  142. #else
  143.   ix_resident (ixembase, 2, a4);
  144. #endif
  145. #endif /* BASECRT0 */
  146.   ixemulbase = ixembase;
  147.   return ix_exec_entry (argc, argv, env, &errno, start_stdio);
  148. }
  149.  
  150. /* this thing is best done with sprintf else, but it has to work without the
  151.  * library as well ;-(
  152.  */
  153. __inline static char *
  154. itoa (int num)
  155. {
  156.   short snum = num;
  157.  
  158.   /* how large can a long get...?? */
  159.   /* Answer (by ljr): best method to use (in terms of portability)
  160.      involves number theory.  The exact number of decimal digits
  161.      needed to store a given number of binary digits is
  162.  
  163.     ceiling ( number_of_binary_digits * log(2) / log(10) )
  164.      or
  165.     ceiling ( number_of_binary_digits * 0.301029996 )
  166.  
  167.      Since sizeof evaluates to the number of bytes a given type takes 
  168.      instead of the number of bits, we need to multiply sizeof (type) by
  169.      CHAR_BIT to obtain the number of bits.  Since an array size specifier
  170.      needs to be integer type, we multiply by 302 and divide by 1000 instead
  171.      of multiplying by 0.301029996.  Finally, we add 1 for the null terminator
  172.      and 1 because we want the ceiling of the function instead of the floor.
  173.      Funny thing about this whole affair is that you really wanted to know
  174.      the size a short could expand to be and not a long...  :-) I know
  175.      comments get out of date, etc.  The nice thing about this method is
  176.      that the size of the array is picked at compile time based upon the
  177.      number of bytes really needed by the local C implementation. */
  178.   static char buf[sizeof snum * CHAR_BIT * 302 / 1000 + 1 + 1];
  179.   char *cp;
  180.   
  181.   KPRINTF (("enter itoa()\n"));
  182.   buf[sizeof buf - 1] = 0;
  183.   for (cp = &buf[sizeof buf - 1]; snum; snum /= 10) 
  184.     *--cp = (snum % 10) + '0';
  185.  
  186.   return cp;
  187. }
  188.  
  189. __inline static char *
  190. pstrcpy (char *start, char *arg)
  191. {
  192.   KPRINTF (("enter pstrcpy()\n"));
  193.   while (*start++=*arg++) ;
  194.   return start-1;
  195. }
  196.  
  197. __inline static char *
  198. build_warn (char *t1, int num1)
  199. {
  200.   static char buf[255];
  201.   char *cp;
  202.   
  203.   KPRINTF (("enter build_warn()\n"));
  204.   cp = pstrcpy (buf, t1);
  205.   cp = pstrcpy (cp, itoa (num1));
  206.  
  207.   return buf;
  208. }
  209.  
  210. static int
  211. ENTRY (void)
  212. {
  213.   register unsigned char *rega0  asm("a0");
  214.   register unsigned long  regd0  asm("d0");
  215. #ifdef BASECRT0
  216.   register int a4;
  217. #endif /* BASECRT0 */
  218.  
  219.   UBYTE *aline = rega0;
  220.   ULONG alen = regd0;
  221.   int res;
  222.  
  223. #ifdef BASECRT0
  224.   struct Library *ibase;
  225.  
  226.   /* needed, so that data can be accessed. ix_resident() might change this
  227.      again afterwards */
  228.   asm volatile ("lea    ___a4_init,a4" : "=r" (a4) : "0" (a4));
  229.   asm volatile ("movel a4,%0" : "=r" (a4) : "0" (a4));
  230.   asm volatile ("movel sp,a4@(___SaveSP:W)");
  231. #else
  232.   asm volatile ("movel sp,___SaveSP");
  233. #endif /* BASECRT0 */
  234.  
  235.   KPRINTF (("enter ENTRY()\n"));
  236.   KPRINTF (("alen = %ld; aline = '%s'\n", alen, aline));
  237. #ifndef BASECRT0
  238.   ixemulbase = OpenLibrary (IX_NAME, IX_VERSION);
  239.   if (ixemulbase)
  240. #else /* BASECRT0 */
  241.   KPRINTF (("ENTRY: a4 = $%lx\n", __geta4()));
  242.  
  243.   ibase = OpenLibrary (IX_NAME, IX_VERSION);
  244.   KPRINTF (("ibase = %lx\n", ibase));
  245.   if (ibase)
  246. #endif /* BASECRT0 */
  247.     {
  248.       /* just warn, in case the user tries to run program which might require
  249.        * more functions than are currently available under this revision. */
  250. #ifndef BASECRT0
  251.       if (ixemulbase->lib_Version == IX_VERSION &&
  252.       ixemulbase->lib_Revision < IX_REVISION)
  253. #else /* BASECRT0 */
  254.       if (ibase->lib_Version == IX_VERSION &&
  255.       ibase->lib_Revision < IX_REVISION)
  256. #endif /* BASECRT0 */
  257.     /* don't need to block signals, they are blocked until after 
  258.          * ix_startup() in current releases */
  259.     __request_msg (build_warn (IX_NAME " warning: needed revision "
  260.                      MSTRING (IX_REVISION) ", current revision ",
  261. #ifndef BASECRT0
  262.                             ixemulbase->lib_Revision), "Continue");
  263. #else /* BASECRT0 */
  264.                             ibase->lib_Revision), "Continue");
  265.  
  266. #ifdef RCRT0
  267.       ix_resident (ibase, 4, a4, dbsize(), __datadata_relocs);
  268. #else
  269.       ix_resident (ibase, 2, a4);
  270. #endif
  271.  
  272.       KPRINTF (("ix_resident: a4 = $%lx\n", __geta4()));
  273. #endif /* BASECRT0 */
  274.  
  275. #ifdef BASECRT0
  276.       ixemulbase = ibase;
  277. #endif /* BASECRT0 */
  278.  
  279.       KPRINTF (("calling ix_startup()\n"));
  280.       res = ix_startup (aline, alen, 
  281.                 expand_cmd_line, default_wb_window, start_stdio, &errno);
  282.  
  283.       CloseLibrary (ixemulbase);
  284.  
  285.     }
  286.   else
  287.     {
  288.       struct Process *me = (struct Process *)((*(struct ExecBase **)4)->ThisTask);
  289.  
  290.       __request_msg ("Need at least version " MSTRING (IX_VERSION)
  291.              " of " IX_NAME ".", "Abort");
  292.       
  293.       /* quickly deal with the WB startup message, as the library couldn't do
  294.        * this for us. Nothing at all is done that isn't necessary to just shutup
  295.        * workbench..*/
  296.       if (! me->pr_CLI)
  297.         {
  298.       Forbid (); 
  299.       ReplyMsg ((WaitPort (& me->pr_MsgPort), GetMsg (& me->pr_MsgPort)));
  300.     }
  301.       
  302.     res = 20;
  303.     }
  304.   /* FIXME: It appears that libnix restores the value of the stack pointer
  305.      from ___SaveSP at a point equivalent to this in the libnix *crt0.S files.
  306.      Do we need to do that also?  -fnf */
  307.   return (res);
  308. }
  309.  
  310. int
  311. start_stdio (int argc, char **argv, char **env)
  312. {
  313.   int res;
  314.   extern void __init_stk (void);
  315. #ifndef BASECRT0
  316.   extern void etext ();
  317.   extern void _mcleanup ();
  318. #else /* BASECRT0 */
  319.  
  320.   KPRINTF (("start_stdio1: a4 = $%lx\n", __geta4()));
  321. #endif /* BASECRT0 */
  322.  
  323. #ifdef DEBUG_VERSION
  324.   KPRINTF (("enter start_stdio()\n"));
  325.   KPRINTF (("argc = %ld (args follow)\n", argc));
  326.   {
  327.     int i;
  328.     for (i = 0; i < argc; i++)
  329.       {
  330.     KPRINTF (("argv[%ld] = '%s'\n", i, argv[i]));
  331.       }
  332.   }
  333. #endif
  334.   
  335.   /* more to follow ;-) */
  336.   ix_get_vars2 (6, &_ctype_, &sys_nerr, &SysBase, &DOSBase, &__sF, &environ);
  337.   environ = env;
  338.  
  339.   /* Initialize the stack checking / stack growth code. */
  340.   __init_stk ();
  341.  
  342. #ifndef BASECRT0
  343. #ifdef MCRT0
  344.   atexit(_mcleanup);
  345.   monstartup(start_stdio, etext);
  346. #endif
  347. #endif /* not BASECRT0 */
  348.  
  349.   res = main (argc, argv, env);
  350.   return res;
  351. }
  352.  
  353. #ifndef BASECRT0
  354. #ifdef CRT0
  355. /*
  356.  * null mcount and moncontrol,
  357.  * just in case some routine is compiled for profiling
  358.  */
  359. asm(".globl mcount");
  360. asm(".globl _moncontrol");
  361. asm("_moncontrol:");
  362. asm("mcount: rts");
  363. #endif CRT0
  364. #endif /* not BASECRT0 */
  365.